//
//  GatewayApi.swift
//  WNLockDemo
//
//  Created by 裴威 on 01/09/2018.
//  Copyright © 2018 wunu. All rights reserved.
//

import Foundation
import Moya


/// 蓝牙网关操作
///
/// - smartGateway: 注册网关
/// - addWhiteList: 锁添加至网关白名单
/// - deleteWhiteList: 锁从网关白名单移除
/// - gatewayId: 删除网关
/// - gatewayList: 获取网关列表
/// - getSmartGateway: 网关详情
/// - gatewayDeviceList: 网关下关联设备列表
/// - sendBleCmd: 网关透传数据
enum GatewayApi {
    case smartGateway(mac: String, name: String, model: String, image: String)
    case addWhiteList(gatewayId: Int, lockId: Int, mac: String)
    case deleteWhiteList(gatewayId: Int, lockId: Int)
    case deleteGateway(gatewayId: Int)
    case gatewayList
    case getSmartGateway(mac: String)
    case gatewayDeviceList(gatewayId: Int)
    case sendBleCmd(lockId: Int, data: String)
}

extension GatewayApi: FxTargetType {
    var path: String {
        switch self {
        case .smartGateway:         return "/weapp/smartGateway"
        case .addWhiteList:         return "/weapp/addWhiteList"
        case .deleteWhiteList:      return "/weapp/deleteWhiteList"
        case .deleteGateway:        return "/weapp/deleteGateway"
        case .gatewayList:          return "/weapp/gatewayList"
        case .getSmartGateway:      return "/weapp/smartGateway"
        case .gatewayDeviceList:    return "/weapp/gatewayDeviceList"
        case .sendBleCmd:           return "/weapp/sendBleCmd"
        }
    }
    var method: Moya.Method {
        switch self {
        case .smartGateway, .addWhiteList, .deleteWhiteList, .sendBleCmd:
            return .post
        case .deleteGateway,.gatewayList, .getSmartGateway, .gatewayDeviceList:
            return .get
        }
    }
    var task: Task {
        switch self {
        case let .smartGateway(mac, name, model, image):
            return .requestParameters(parameters: ["mac": mac, "name": name, "model": model, "image": image], encoding: URLEncoding.default)
        case let .addWhiteList(gatewayId, lockId, mac):
            return .requestParameters(parameters: ["gatewayId": gatewayId, "lockId": lockId, "mac": mac], encoding: URLEncoding.default)
        case let .deleteGateway(gatewayId), let .gatewayDeviceList(gatewayId):
            return .requestParameters(parameters: ["gatewayId": gatewayId], encoding: URLEncoding.default)
        case .gatewayList:
            return .requestPlain
        case let .getSmartGateway(mac):
            return .requestParameters(parameters: ["mac": mac], encoding: URLEncoding.default)
        case let .sendBleCmd(lockId, data):
            return .requestParameters(parameters: ["lockId": lockId, "data": data], encoding: URLEncoding.default)
        case .deleteWhiteList(let gatewayId, let lockId):
            return .requestParameters(parameters: ["gatewayId": gatewayId, "lockId": lockId], encoding: URLEncoding.default)
        }
    }
}
